public class offer24
{
    public boolean VerifySquenceOfBST(int [] sequence)
    {
        if(sequence==null)
            return false;
        if(sequence.length==0)
            return false;
        if(sequence.length<=2)
            return true;

        return Verify(sequence,0,sequence.length-1);
    }

    public boolean Verify(int []sequence,int start,int end)
    {
        int i,n;
        n=sequence[end];
        int first=start;
        int last=end;

        int lend;

        i=start;
        while(sequence[i]<n)
            i++;
        lend=i-1;
        for(i=lend+1;i<=end-1;i++)
            if(sequence[i]<=n)
                break;

        if(i!=end)
            return false;
        boolean re;
        if(lend-start>=2)
        {
            re = Verify(sequence, start, lend);
            if (re == false)
                return false;
        }

        int rend=end-1;
        int rstart=lend+1;
        if(rend-rstart>=2)
        {
            re = Verify(sequence, rstart, rend);
            if (re == false)
                return false;
        }
        return true;
    }

}